package com.hw.one.dam2.dao;

import java.text.SimpleDateFormat;
import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import com.hw.one.dam2.common.DamConstant;
import com.hw.one.dam2.entity.PollutionIndustryEntity;
import com.hw.one.dam2.param.PollutionIndustryParam;

/**
 * 标准站 小时数据，日数据
 * 
 * @author admin
 *
 */
@Repository(value = "industryPollutionDao")
public class IndustryPollutionDao implements IIndustryPollutionDao {

	// 在Spring中注册的 mongoDB Template
	@Autowired
	private MongoTemplate mongoTemplateStandardSiteData;

	@Override
	public List<PollutionIndustryEntity> findIndustryPollutionBySiteCodeAndTime(PollutionIndustryParam paramPO,
			String collectionName) {
		String startTime = paramPO.getStartTime();
		String endTime = paramPO.getEndTime();
		// 查询条件 增加时间查询条件
		Criteria criteria = Criteria.where("MONITORTIME");

		criteria.gte(startTime).lte(endTime);
		criteria.and("PSCode").in(paramPO.getPSCodeList());
		// criteria.and("OutputCode").in(paramPO.getOutputCodeList());

		// 创建查询对象
		Query query = new Query(criteria);
		query.with(new Sort(Sort.Direction.DESC, "MONITORTIME"));
		List<PollutionIndustryEntity> ipList = mongoTemplateStandardSiteData.find(query, PollutionIndustryEntity.class,
				collectionName);
		return ipList;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public List<Map> findPollutionNewHourDataByPSCodeAndOutputCodes(List<String> siteCodeList,
			List<String> outputCodeList) throws Exception {

		Map newData = mongoTemplateStandardSiteData
				.findOne(
						new Query(Criteria.where("PSCode").in(siteCodeList).and("OutputCode").in(outputCodeList))
								.with(new Sort(Sort.Direction.DESC, "MONITORTIME")),
						Map.class, "T_DATA_GASPOLLUTION_HOUR_S_COLLECT");

		Object timeObj = newData.get("MONITORTIME");
		SimpleDateFormat sdf = new SimpleDateFormat(DamConstant.SDF_YMD_HMS);
		Date date = sdf.parse(timeObj.toString());
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);

		calendar.add(Calendar.HOUR_OF_DAY, -1);

		String startTime = sdf.format(calendar.getTime());
		// 查询条件 增加时间查询条件
		Criteria criteria = Criteria.where("MONITORTIME").is(startTime);
		criteria.and("PSCode").in(siteCodeList);
		criteria.and("OutputCode").in(outputCodeList);

		Query query = new Query(criteria);
		query.with(new Sort(Sort.Direction.DESC, "MONITORTIME"));

		List<Map> ipList = mongoTemplateStandardSiteData.find(query, Map.class, "T_DATA_GASPOLLUTION_HOUR_S_COLLECT");

		return ipList;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public List<Map> findLast12HourIndustryHourDataBySiteCode(String siteCode, List<String> outputCodeList)
			throws Exception {
		Map newData = mongoTemplateStandardSiteData
				.findOne(
						new Query(Criteria.where("PSCode").is(siteCode).and("OutputCode").in(outputCodeList))
								.with(new Sort(Sort.Direction.DESC, "MONITORTIME")),
						Map.class, "T_DATA_GASPOLLUTION_HOUR_S_COLLECT");
		if (null == newData) {
			return new ArrayList<>();
		}
		Object timeObj = newData.get("MONITORTIME");

		SimpleDateFormat sdf = new SimpleDateFormat(DamConstant.SDF_YMD_HMS);
		Date date = sdf.parse(timeObj.toString());
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);

		calendar.add(Calendar.HOUR_OF_DAY, -12);

		String startTime = sdf.format(calendar.getTime());

		// 查询条件 增加时间查询条件
		Criteria criteria = Criteria.where("MONITORTIME").gte(startTime).lte(timeObj);
		criteria.and("PSCode").is(siteCode);
		criteria.and("OutputCode").in(outputCodeList);

		Query query = new Query(criteria);
		query.with(new Sort(Sort.Direction.ASC, "MONITORTIME"));

		List<Map> ipList = mongoTemplateStandardSiteData.find(query, Map.class, "T_DATA_GASPOLLUTION_HOUR_S_COLLECT");

		return ipList;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public List<Map> findIndustryPollutionBySiteCodeAndSDTime(PollutionIndustryParam paramPO, String collectionName) {
		String startTime = paramPO.getStartTime();
		String endTime = paramPO.getEndTime();
		// 查询条件 增加时间查询条件
		Criteria criteria = Criteria.where("MONITORTIME");

		criteria.gte(startTime).lte(endTime);
		criteria.and("PSCode").is(paramPO.getPSCodeList().get(0));
		criteria.and("OutputCode").in(paramPO.getOutputCodeList());

		// 创建查询对象
		Query query = new Query(criteria);
		query.with(new Sort(Sort.Direction.ASC, "MONITORTIME"));
		List<Map> ipList = mongoTemplateStandardSiteData.find(query, Map.class,
				collectionName);
		return ipList;
	}

}
